package com.jz.community.moduleshopping.integralGoods.ui;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jz.community.basecomm.base.BaseHtml5Activity;
import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.base.BaseX5WebActivity;
import com.jz.community.basecomm.constant.Constant;
import com.jz.community.basecomm.mvp.BaseMvpPresenter;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.utils.BaseImageLoaderUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.R2;
import com.jz.community.moduleshopping.orderDetail.bean.OrderDetailBean;
import com.jz.community.moduleshopping.orderDetail.task.OrderDetailTask;
import com.jz.community.moduleshopping.orderDetail.ui.OrderTrackActivity;

import butterknife.BindView;

/**
 * Created by Tony.Fan on 2018/7/2 15:27
 */
public class IntegralSuccessActivity extends BaseMvpActivity {

    @BindView(R2.id.iv_shop_goods_photo)
    ImageView ivShopGoodsPhoto;
    @BindView(R2.id.tv_shop_goods_name)
    TextView tvShopGoodsName;
    @BindView(R2.id.tv_shop_goods_price)
    TextView tvShopGoodsPrice;
    @BindView(R2.id.tv_shop_goods_buy_count)
    TextView tvShopGoodsBuyCount;
    @BindView(R2.id.tv_shop_goods_description)
    TextView tvShopGoodsDescription;
    @BindView(R2.id.integral_point_tv)
    TextView integralPointTv;
    @BindView(R2.id.tv_transaction_number)
    TextView tvTransactionNumber;
    @BindView(R2.id.ll_pay_price_number)
    LinearLayout llPayPriceNumber;
    @BindView(R2.id.integral_order_number_tv)
    TextView integralOrderNumberTv;
    @BindView(R2.id.integral_track_tv)
    TextView integralTrackTv;
    @BindView(R2.id.integral_day_tv)
    TextView integralDayTv;
    @BindView(R2.id.ll_coupon_dialog)
    LinearLayout llCouponDialog;
    @BindView(R2.id.tv_online_service)
    TextView tvOnlineService;
    @BindView(R2.id.ll_online_service)
    LinearLayout llOnlineService;
    @BindView(R2.id.tv_tel_service)
    TextView tvTelService;
    @BindView(R2.id.ll_tel_service)
    LinearLayout llTelService;
    @BindView(R2.id.title_back_left)
    ImageView titleBackLeft;
    @BindView(R2.id.title_name)
    TextView titleName;
    @BindView(R2.id.title_right)
    TextView titleRight;
    @BindView(R2.id.order_sucess_integral_tv)
    TextView orderSucessIntegralTv;
    @BindView(R2.id.title_toolbar)
    Toolbar titleToolbar;
    private String orderId;
    private int sendType;
    private OrderDetailBean mOrderDetailBean;

    @Override
    protected int getContentView() {
        return R.layout.module_shopping_integral_goods_success_layout;
    }

    @Override
    protected void initView() {
        super.initView();
        setImmersionBar(titleToolbar);
        SHelper.gone(titleName);
        initTitle("", "");
    }

    @Override
    protected void initDatas() {
        orderId = getIntent().getStringExtra("orderId");
        sendType = getIntent().getIntExtra("sendType", -1);

        handleIntegralSucessMsg();

        if (!Preconditions.isNullOrEmpty(orderId)) {
            getOrderDetailData();
        }
    }

    /**
     * 快递显示发货时间、自提隐藏
     */
    private void handleIntegralSucessMsg() {
        if (sendType == 2) {
            SHelper.gone(orderSucessIntegralTv);
        } else {
            SHelper.vis(orderSucessIntegralTv);
        }
    }

    @Override
    protected void addListener() {
        llOnlineService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(IntegralSuccessActivity.this, BaseX5WebActivity.class)
                        .putExtra("url", Constant.CUSTOM_SERVICE_URL)
                        .putExtra("title", "在线客服"));
            }
        });
        llTelService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent5 = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + 9521133));
                startActivity(intent5);
            }
        });
        integralTrackTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(IntegralSuccessActivity.this, OrderTrackActivity.class);
                intent.putExtra("orderId", orderId);
                intent.putExtra("address", mOrderDetailBean.getAddress());
                intent.putExtra("sendType", mOrderDetailBean.getSendType());
                intent.putExtra("phone", mOrderDetailBean.getAddressPhone());
                intent.putExtra("expressName", mOrderDetailBean.getExpressName());
                intent.putExtra("expressCode", mOrderDetailBean.getExpressNumber());
                startActivity(intent);
            }
        });
    }

    /**
     * 获取订单详情
     */
    private void getOrderDetailData() {
        new OrderDetailTask(this, new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {
                IntegralSuccessActivity.this.setDatas((OrderDetailBean) o);
            }
        }).execute(orderId);
    }

    private void setDatas(OrderDetailBean orderDetailBean) {
        if (!Preconditions.isNullOrEmpty(orderDetailBean)) {
            mOrderDetailBean = orderDetailBean;
            showIntegralGoodsInfo(orderDetailBean.getOrderItemList().get(0));
            tvTransactionNumber.setText("-" + orderDetailBean.getIntegral());
            integralOrderNumberTv.setText(orderDetailBean.getOrderId());
            integralDayTv.setText(orderDetailBean.getCreateDate());
        }
    }

    private void showIntegralGoodsInfo(OrderDetailBean.OrderItemListBean mOrderItemInfo) {
        BaseImageLoaderUtils.getInstance().loadDefaltImage(this, ivShopGoodsPhoto, mOrderItemInfo.getGoodsImg());
        tvShopGoodsName.setText(mOrderItemInfo.getGoodsName());
        tvShopGoodsPrice.setText(mOrderItemInfo.getDiscountPrice() + "积分");
        tvShopGoodsBuyCount.setText("x" + mOrderItemInfo.getGoodsCount());
        tvShopGoodsDescription.setText(mOrderItemInfo.getSkuName());
    }

    @Override
    protected BaseMvpPresenter createPresenter() {
        return null;
    }
}
